package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.bean.People;
import com.example.myapplication.util.DBAdapter;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText editTextName;
    private EditText editTextAge;
    private EditText editTextHeight;
    private EditText editTextId;
    private Button buttonAdd;
    private Button buttonShowAll;
    private Button buttonDeleteAll;
    private Button buttonDeleteById;
    private Button buttonGetById;
    private Button buttonUpdateById;
    private TextView textViewShow;
    private DBAdapter dbAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextName = findViewById(R.id.editText_name);
        editTextAge = findViewById(R.id.editText_age);
        editTextHeight = findViewById(R.id.editText_height);
        editTextId = findViewById(R.id.editText_id);
        buttonAdd = findViewById(R.id.button_add);
        buttonShowAll = findViewById(R.id.button_showAll);
        buttonDeleteAll = findViewById(R.id.button_deleteAll);
        buttonGetById = findViewById(R.id.button_getById);
        buttonUpdateById = findViewById(R.id.button_updateById);
        buttonDeleteById = findViewById(R.id.button_deleteById);
        textViewShow = findViewById(R.id.textView_show);
        //得到数据库实例
        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
        //添加
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                People people = new People();
                people.setName(editTextName.getText().toString());
                people.setAge(Integer.parseInt(editTextAge.getText().toString()));
                people.setHeight(Float.parseFloat(editTextHeight.getText().toString()));
                long res = dbAdapter.insert(people);
                if(res > 0){
                    Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //全部显示
        buttonShowAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<People> peopleList = dbAdapter.getAll();
                if(peopleList == null){
                    textViewShow.setText("暂无数据");
                    return;
                }
                StringBuilder builder = new StringBuilder();
                for(People people : peopleList){
                    builder.append(people.toString());
                    builder.append("\n");
                }
                textViewShow.setText(builder.toString());
            }
        });
        //全部删除
        buttonDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long res = dbAdapter.deleteAllData();
                Toast.makeText(MainActivity.this, "删除" + res + "条记录", Toast.LENGTH_SHORT).show();
            }
        });

        //根据id删除
        buttonDeleteById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    long id = Long.parseLong(editTextId.getText().toString());
                    long res = dbAdapter.deleteById(id);
                    if(res > 0){
                        Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this, "id不存在", Toast.LENGTH_SHORT).show();
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(MainActivity.this, "整形id", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //根据id查询
        buttonGetById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    long id = Long.parseLong(editTextId.getText().toString());
                    People people = dbAdapter.getById(id);
                    if(people != null){
                        textViewShow.setText(people.toString());
                    }else{
                        textViewShow.setText("未找到");
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(MainActivity.this, "整形id", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //根据id更新
        buttonUpdateById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    People people = new People();
                    int id = Integer.parseInt(editTextId.getText().toString());
                    people.setId(id);
                    people.setName(editTextName.getText().toString());
                    people.setAge(Integer.parseInt(editTextAge.getText().toString()));
                    people.setHeight(Float.parseFloat(editTextHeight.getText().toString()));
                    long res = dbAdapter.updateById(people);
                    Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                } catch (NumberFormatException e) {
                    Toast.makeText(MainActivity.this, "整形id", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dbAdapter.close();
    }
}